<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>【动态图版】北宋、明清进士空间可视化分析 - Libido Chateau</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="迷幻主义搬砖号子" /><meta name="description" content="利用animiation、ECharts2和leaflet将进士地理信息动态可视化到地图上。" /><meta name="keywords" content="进士, 北宋, 明清, 空间, 地理, GIS, CBDB" />


<meta name="baidu-site-verification" content="k5CktWnwfR" />
<meta name="google-site-verification" content="lO7fVN8Jm2sjiBOcR2knmNBKjtWcxS6KbuZYl2yELwA" />


<meta name="generator" content="Hugo 0.108.0 with theme even" />


<link rel="canonical" href="https://madlogos.github.io/post/song_ming_qing_jinshi_interactive/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<link href="/sass/main.min.67ad89b7ca43747ca0757fa0c720e4a26dfb092d0c625a7e1f3b937263f7d48d.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" integrity="sha256-Vzbj7sDDS/woiFS3uNKo8eIuni59rjyNGtXfstRzStA=" crossorigin="anonymous">
<link rel="stylesheet" href="/css/custom.css">


<meta property="og:title" content="【动态图版】北宋、明清进士空间可视化分析" />
<meta property="og:description" content="利用animiation、ECharts2和leaflet将进士地理信息动态可视化到地图上。" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://madlogos.github.io/post/song_ming_qing_jinshi_interactive/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2017-04-30T00:00:00+00:00" />
<meta property="article:modified_time" content="2021-05-09T21:28:26+08:00" />


<meta itemprop="name" content="【动态图版】北宋、明清进士空间可视化分析">
<meta itemprop="description" content="利用animiation、ECharts2和leaflet将进士地理信息动态可视化到地图上。"><meta itemprop="datePublished" content="2017-04-30T00:00:00+00:00" />
<meta itemprop="dateModified" content="2021-05-09T21:28:26+08:00" />
<meta itemprop="wordCount" content="3604">
<meta itemprop="keywords" content="进士,地理分析,交互图," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="【动态图版】北宋、明清进士空间可视化分析"/>
<meta name="twitter:description" content="利用animiation、ECharts2和leaflet将进士地理信息动态可视化到地图上。"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">Libido Chateau</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">Libido Chateau</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/">关于</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">【动态图版】北宋、明清进士空间可视化分析</h1>

      <div class="post-meta">
        <span class="post-time"> 2017-04-30 </span>
        <div class="post-category">
            <a href="/categories/%E5%8F%AF%E8%A7%86%E5%8C%96/"> 可视化 </a>
            </div>
          <span class="more-meta"> 约 3604 字 </span>
          <span class="more-meta"> 预计阅读 8 分钟 </span>
        <span id="busuanzi_container_page_pv" class="more-meta"> <span id="busuanzi_value_page_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次阅读 </span>
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#动态图">动态图</a></li>
        <li><a href="#动画">动画</a></li>
        <li><a href="#交互图">交互图</a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <div class="admonition warning"><p class="admonition-title">注意</p>
<p>如果浏览器提示本文加载了不安全的脚本，请点允许。</p>
</div>
<p>今次主题比较简单。上个话题留了点冷饭，看起来还没馊，咱敲个鸡蛋炒个蛋炒饭。</p>
<p>炒个什么蛋炒饭呢？——动态图(dynamic charts)。这也是应留言要求额外发的番外。基本和分析关系不大，纯粹是可视化范畴。</p>
<h3 id="动态图">动态图</h3>
<p>通常我们看到的都是静态图，最常见的是.jpg、.png这类位图，逼格高一点的会用到.svg矢量图。但它们都是死图，所有图形元素都不会动。某些情况下，我们不仅要把统计结果映射到特定的视觉通道，还希望表现其历时性，或者允许用户自己进行挖掘。这就需要让图形部件动起来。</p>
<ul>
<li>一种方法是做成动画(animation)。</li>
</ul>
<p>R有个名包，叫animation，可以用它压制.gif，用在社交媒体效果足够醒目。它的基本思路就是拿出一维来映射时间，基于时间点对数据切片、统计、制图，最后把静态图们合成一个动画。</p>
<ul>
<li>还有一种方法是交互图(interactive charts)。</li>
</ul>
<p>把统计数据绑定到JavaScript控件上，定义好交互方法，用户即可在网页上通过控件操作来调整视觉呈现（切片、缩放、改变类型等）。RStudio发过一个工具框架包htmlwidgets，可以很方便地把已有的JavaScript可视化库移植到R。我们今天就要用到其中的两个：ECharts2和leaflet。</p>
<p>如果再进一步，就是数据交互面板了。R有shiny及其系列衍生品，比如flexboard。想象一下作战室交互图仪表盘面板，几行命令就做出来了。简直酷炫。但是这需要部署在shiny服务器上。</p>
<h3 id="动画">动画</h3>
<p>还是用前次的数据集，再画一下各朝的进士地理热图。但这次变个花样，我们先把北宋、明、清都按前、中、后分一下期。</p>
<ul>
<li>用cut函数把连续变量切分成分段因子；</li>
<li>用sprintf格式化因子标签，否则会自动套用前开后闭科学计数法区间样式，表示年代比较怪；</li>
<li>各朝的分期不见得都对，我就随便分了下：
<ul>
<li>北宋(960-1127)：以1021、1085分期，大体对应真宗/仁宗、神宗/哲宗；</li>
<li>明朝(1668-1644)：以1434、1572分期，大体对应宣宗(宣德)/英宗、穆宗(隆庆)/神宗(万历)；</li>
<li>清朝(1644-1911)：以1735、1850分期，大体对应世宗(雍正)/高宗(乾隆)、宣宗(道光)/文宗(咸丰)。</li>
</ul>
</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="nf">library</span><span class="p">(</span><span class="n">animation</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">nsong.js</span><span class="o">$</span><span class="n">period</span> <span class="o">&lt;-</span> <span class="nf">cut</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">    <span class="n">nsong.js</span><span class="o">$</span><span class="n">EntryYear</span><span class="p">,</span> <span class="nf">c</span><span class="p">(</span><span class="m">960</span><span class="p">,</span> <span class="m">1021</span><span class="p">,</span> <span class="m">1085</span><span class="p">,</span> <span class="m">1127</span><span class="p">),</span> <span class="n">include.lowest</span><span class="o">=</span><span class="kc">TRUE</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="n">labels</span><span class="o">=</span><span class="nf">paste</span><span class="p">(</span><span class="nf">sprintf</span><span class="p">(</span><span class="s">&#34;%4d&#34;</span><span class="p">,</span> <span class="n">cutyears[1</span><span class="o">:</span><span class="p">(</span><span class="nf">length</span><span class="p">(</span><span class="n">cutyears</span><span class="p">)</span><span class="m">-1</span><span class="p">)</span><span class="n">]</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">                 <span class="nf">sprintf</span><span class="p">(</span><span class="s">&#34;%4d&#34;</span><span class="p">,</span> <span class="n">cutyears[2</span><span class="o">:</span><span class="nf">length</span><span class="p">(</span><span class="n">cutyears</span><span class="p">)</span><span class="n">]</span><span class="p">),</span> <span class="n">sep</span><span class="o">=</span><span class="s">&#34;-&#34;</span><span class="p">))</span>
</span></span><span class="line"><span class="cl"><span class="n">ming.js</span><span class="o">$</span><span class="n">period</span> <span class="o">&lt;-</span> <span class="nf">cut</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">    <span class="n">ming.js</span><span class="o">$</span><span class="n">EntryYear</span><span class="p">,</span> <span class="nf">c</span><span class="p">(</span><span class="m">1368</span><span class="p">,</span> <span class="m">1434</span><span class="p">,</span> <span class="m">1572</span><span class="p">,</span> <span class="m">1644</span><span class="p">),</span> <span class="n">include.lowest</span><span class="o">=</span><span class="kc">TRUE</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="n">labels</span><span class="o">=</span><span class="nf">paste</span><span class="p">(</span><span class="nf">sprintf</span><span class="p">(</span><span class="s">&#34;%4d&#34;</span><span class="p">,</span> <span class="n">cutyears[1</span><span class="o">:</span><span class="p">(</span><span class="nf">length</span><span class="p">(</span><span class="n">cutyears</span><span class="p">)</span><span class="m">-1</span><span class="p">)</span><span class="n">]</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">                 <span class="nf">sprintf</span><span class="p">(</span><span class="s">&#34;%4d&#34;</span><span class="p">,</span> <span class="n">cutyears[2</span><span class="o">:</span><span class="nf">length</span><span class="p">(</span><span class="n">cutyears</span><span class="p">)</span><span class="n">]</span><span class="p">),</span> <span class="n">sep</span><span class="o">=</span><span class="s">&#34;-&#34;</span><span class="p">))</span>
</span></span><span class="line"><span class="cl"><span class="n">qing.js</span><span class="o">$</span><span class="n">period</span> <span class="o">&lt;-</span> <span class="nf">cut</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">    <span class="n">qing.js</span><span class="o">$</span><span class="n">EntryYear</span><span class="p">,</span> <span class="nf">c</span><span class="p">(</span><span class="m">1644</span><span class="p">,</span> <span class="m">1735</span><span class="p">,</span> <span class="m">1850</span><span class="p">,</span> <span class="m">1911</span><span class="p">),</span> <span class="n">include.lowest</span><span class="o">=</span><span class="kc">TRUE</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="n">labels</span><span class="o">=</span><span class="nf">paste</span><span class="p">(</span><span class="nf">sprintf</span><span class="p">(</span><span class="s">&#34;%4d&#34;</span><span class="p">,</span> <span class="n">cutyears[1</span><span class="o">:</span><span class="p">(</span><span class="nf">length</span><span class="p">(</span><span class="n">cutyears</span><span class="p">)</span><span class="m">-1</span><span class="p">)</span><span class="n">]</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">                 <span class="nf">sprintf</span><span class="p">(</span><span class="s">&#34;%4d&#34;</span><span class="p">,</span> <span class="n">cutyears[2</span><span class="o">:</span><span class="nf">length</span><span class="p">(</span><span class="n">cutyears</span><span class="p">)</span><span class="n">]</span><span class="p">),</span> <span class="n">sep</span><span class="o">=</span><span class="s">&#34;-&#34;</span><span class="p">))</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>各自切片统计，再做成.gif动画。这里设置动画间隔为2秒，每一帧大小都是640×480像素。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="n">oopt</span> <span class="o">&lt;-</span> <span class="nf">ani.options</span><span class="p">(</span><span class="n">interval</span><span class="o">=</span><span class="m">2</span><span class="p">,</span> <span class="n">ani.width</span><span class="o">=</span><span class="m">640</span><span class="p">,</span> <span class="n">ani.height</span><span class="o">=</span><span class="m">480</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nf">saveGIF</span><span class="p">({</span>
</span></span><span class="line"><span class="cl">    <span class="nf">for </span><span class="p">(</span><span class="n">i</span> <span class="n">in</span> <span class="nf">levels</span><span class="p">(</span><span class="n">nsong.js</span><span class="o">$</span><span class="n">period</span><span class="p">)){</span>
</span></span><span class="line"><span class="cl">        <span class="nf">print</span><span class="p">(</span><span class="n">p.song</span> <span class="o">+</span> <span class="nf">stat_density_2d</span><span class="p">(</span><span class="nf">aes</span><span class="p">(</span><span class="n">x_coord</span><span class="p">,</span> <span class="n">y_coord</span><span class="p">,</span> <span class="n">fill</span><span class="o">=</span><span class="n">..level..</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">        <span class="n">data</span><span class="o">=</span><span class="n">nsong.js[nsong.js</span><span class="o">$</span><span class="n">period</span><span class="o">==</span><span class="n">i</span><span class="p">,</span><span class="n">]</span><span class="p">,</span> <span class="n">geom</span><span class="o">=</span><span class="s">&#34;polygon&#34;</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="m">0.5</span><span class="p">)</span> <span class="o">+</span>
</span></span><span class="line"><span class="cl">        <span class="nf">scale_fill_gradient</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="s">&#34;cyan&#34;</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="s">&#34;darkblue&#34;</span><span class="p">)</span><span class="o">+</span>
</span></span><span class="line"><span class="cl">        <span class="nf">ggtitle</span><span class="p">(</span><span class="nf">paste0</span><span class="p">(</span><span class="s">&#34;北宋进士来源地 (&#34;</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="s">&#34;)&#34;</span><span class="p">)))</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">},</span> <span class="s">&#34;song.gif&#34;</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><figure class="center"><img src="https://gh-1251443721.cos.ap-chengdu.myqcloud.com/2017/0430/song.gif"/><figcaption>
            <h4>图 | 北宋进士来源地</h4>
        </figcaption>
</figure>
<p>很明显，前期京畿还很有优势，仁宗开始江南大盛，到神宗以后就是闽人大盛。热力分布持续东南移。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="nf">saveGIF</span><span class="p">({</span>
</span></span><span class="line"><span class="cl">    <span class="nf">for </span><span class="p">(</span><span class="n">i</span> <span class="n">in</span> <span class="nf">levels</span><span class="p">(</span><span class="n">ming.js</span><span class="o">$</span><span class="n">period</span><span class="p">)){</span>
</span></span><span class="line"><span class="cl">        <span class="nf">print</span><span class="p">(</span><span class="n">p.ming</span> <span class="o">+</span> <span class="nf">stat_density_2d</span><span class="p">(</span><span class="nf">aes</span><span class="p">(</span><span class="n">x_coord</span><span class="p">,</span> <span class="n">y_coord</span><span class="p">,</span> <span class="n">fill</span><span class="o">=</span><span class="n">..level..</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">        <span class="n">data</span><span class="o">=</span><span class="n">ming.js[ming.js</span><span class="o">$</span><span class="n">period</span><span class="o">==</span><span class="n">i</span><span class="p">,</span><span class="n">]</span><span class="p">,</span> <span class="n">geom</span><span class="o">=</span><span class="s">&#34;polygon&#34;</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="m">0.5</span><span class="p">)</span> <span class="o">+</span>
</span></span><span class="line"><span class="cl">        <span class="nf">scale_fill_gradient</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="s">&#34;cyan&#34;</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="s">&#34;darkblue&#34;</span><span class="p">)</span><span class="o">+</span>
</span></span><span class="line"><span class="cl">        <span class="nf">ggtitle</span><span class="p">(</span><span class="nf">paste0</span><span class="p">(</span><span class="s">&#34;明朝进士来源地 (&#34;</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="s">&#34;)&#34;</span><span class="p">)))</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">},</span> <span class="s">&#34;ming.gif&#34;</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><figure class="center"><img src="https://gh-1251443721.cos.ap-chengdu.myqcloud.com/2017/0430/ming.gif"/><figcaption>
            <h4>图 | 明代进士来源地</h4>
        </figcaption>
</figure>
<p>明和北宋热力分布是反向变动的，神宗以后进士来源地更弥散了。赣闽次第没落，而京畿有了起色。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="nf">saveGIF</span><span class="p">({</span>
</span></span><span class="line"><span class="cl">    <span class="nf">for </span><span class="p">(</span><span class="n">i</span> <span class="n">in</span> <span class="nf">levels</span><span class="p">(</span><span class="n">qing.js</span><span class="o">$</span><span class="n">period</span><span class="p">)){</span>
</span></span><span class="line"><span class="cl">        <span class="nf">print</span><span class="p">(</span><span class="n">p.qing</span> <span class="o">+</span> <span class="nf">stat_density_2d</span><span class="p">(</span><span class="nf">aes</span><span class="p">(</span><span class="n">x_coord</span><span class="p">,</span> <span class="n">y_coord</span><span class="p">,</span> <span class="n">fill</span><span class="o">=</span><span class="n">..level..</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">        <span class="n">data</span><span class="o">=</span><span class="n">qing.js[qing.js</span><span class="o">$</span><span class="n">period</span><span class="o">==</span><span class="n">i</span><span class="p">,</span><span class="n">]</span><span class="p">,</span> <span class="n">geom</span><span class="o">=</span><span class="s">&#34;polygon&#34;</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="m">0.5</span><span class="p">)</span> <span class="o">+</span>
</span></span><span class="line"><span class="cl">        <span class="nf">scale_fill_gradient</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="s">&#34;cyan&#34;</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="s">&#34;darkblue&#34;</span><span class="p">)</span><span class="o">+</span>
</span></span><span class="line"><span class="cl">        <span class="nf">ggtitle</span><span class="p">(</span><span class="nf">paste0</span><span class="p">(</span><span class="s">&#34;清朝进士来源地 (&#34;</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="s">&#34;)&#34;</span><span class="p">)))</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">},</span> <span class="s">&#34;qing.gif&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nf">ani.options</span><span class="p">(</span><span class="n">oopt</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><figure class="center"><img src="https://gh-1251443721.cos.ap-chengdu.myqcloud.com/2017/0430/qing.gif"/><figcaption>
            <h4>图 | 清代进士来源地</h4>
        </figcaption>
</figure>
<p>清和明类似，中前期江南独大，鸦片战争以后分布逐渐弥散。</p>
<p>和静态图相比，引入历时维度后，信息量明显就更大了。</p>
<h3 id="交互图">交互图</h3>
<h4 id="个体散点图">个体散点图</h4>
<p>在哈佛人文地理平台上，进士数据集本身就以散点和热力图形式展示。这有一个问题：同一个来源地的坐标是完全一样的，所以这些个体在空间上都叠在一起，无法区分。在用leaflet再现散点分布之前，我们用随机数把他们打散开。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="n">nsong.js</span><span class="o">$</span><span class="n">long</span> <span class="o">&lt;-</span> <span class="n">nsong.js</span><span class="o">$</span><span class="n">x_coord</span> <span class="o">+</span> <span class="nf">rnorm</span><span class="p">(</span><span class="nf">nrow</span><span class="p">(</span><span class="n">nsong.js</span><span class="p">))</span><span class="o">/</span><span class="m">100</span>
</span></span><span class="line"><span class="cl"><span class="n">nsong.js</span><span class="o">$</span><span class="n">lat</span> <span class="o">&lt;-</span> <span class="n">nsong.js</span><span class="o">$</span><span class="n">y_coord</span> <span class="o">+</span> <span class="nf">rnorm</span><span class="p">(</span><span class="nf">nrow</span><span class="p">(</span><span class="n">nsong.js</span><span class="p">))</span><span class="o">/</span><span class="m">100</span>
</span></span><span class="line"><span class="cl"><span class="n">ming.js</span><span class="o">$</span><span class="n">long</span> <span class="o">&lt;-</span> <span class="n">ming.js</span><span class="o">$</span><span class="n">x_coord</span> <span class="o">+</span> <span class="nf">rnorm</span><span class="p">(</span><span class="nf">nrow</span><span class="p">(</span><span class="n">ming.js</span><span class="p">))</span><span class="o">/</span><span class="m">100</span>
</span></span><span class="line"><span class="cl"><span class="n">ming.js</span><span class="o">$</span><span class="n">lat</span> <span class="o">&lt;-</span> <span class="n">ming.js</span><span class="o">$</span><span class="n">y_coord</span> <span class="o">+</span> <span class="nf">rnorm</span><span class="p">(</span><span class="nf">nrow</span><span class="p">(</span><span class="n">ming.js</span><span class="p">))</span><span class="o">/</span><span class="m">100</span>
</span></span><span class="line"><span class="cl"><span class="n">qing.js</span><span class="o">$</span><span class="n">long</span> <span class="o">&lt;-</span> <span class="n">qing.js</span><span class="o">$</span><span class="n">x_coord</span> <span class="o">+</span> <span class="nf">rnorm</span><span class="p">(</span><span class="nf">nrow</span><span class="p">(</span><span class="n">qing.js</span><span class="p">))</span><span class="o">/</span><span class="m">100</span>
</span></span><span class="line"><span class="cl"><span class="n">qing.js</span><span class="o">$</span><span class="n">lat</span> <span class="o">&lt;-</span> <span class="n">qing.js</span><span class="o">$</span><span class="n">y_coord</span> <span class="o">+</span> <span class="nf">rnorm</span><span class="p">(</span><span class="nf">nrow</span><span class="p">(</span><span class="n">qing.js</span><span class="p">))</span><span class="o">/</span><span class="m">100</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>这样一来，低级缩放时这些点还是叠在一起的，只要持续放大，就能散开。这当然很不精确，但是我感觉能忍。</p>
<p>构造一个工作函数<code>make_leaflet</code>，把参数传进去，就能画个leaflet交互图出来。leaflet的语法很简单，就是个leaflet js库的wrapper(咋翻译？包裹函数？)，参数列表几乎都是从API文档继承来的。基本的使用顺序是初始化-&gt;添加地图图层-&gt;添加主题图层-&gt;添加交互控件。我没多玩花活，加了个label和popup。</p>
<p>leaflet是基于svg矢量图的，源代码里就包含很多矢量代码。加上这里数据点多，文件尺寸就都很大。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="nf">library</span><span class="p">(</span><span class="n">leaflet</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">make_leaflet</span> <span class="o">&lt;-</span> <span class="nf">function</span><span class="p">(</span><span class="n">refMap</span><span class="p">,</span> <span class="n">dyn</span><span class="p">,</span> <span class="n">bgColor</span><span class="o">=</span><span class="s">&#34;red&#34;</span><span class="p">,</span> <span class="n">dataset</span><span class="p">,</span> <span class="n">cutyears</span><span class="p">){</span>
</span></span><span class="line"><span class="cl">    <span class="n">dataset</span><span class="o">$</span><span class="n">period</span> <span class="o">&lt;-</span> <span class="nf">cut</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">        <span class="n">dataset</span><span class="o">$</span><span class="n">EntryYear</span><span class="p">,</span> <span class="n">cutyears</span><span class="p">,</span> <span class="n">include.lowest</span><span class="o">=</span><span class="kc">TRUE</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">        <span class="n">labels</span><span class="o">=</span><span class="nf">paste</span><span class="p">(</span><span class="nf">sprintf</span><span class="p">(</span><span class="s">&#34;%4d&#34;</span><span class="p">,</span> <span class="n">cutyears[1</span><span class="o">:</span><span class="p">(</span><span class="nf">length</span><span class="p">(</span><span class="n">cutyears</span><span class="p">)</span><span class="m">-1</span><span class="p">)</span><span class="n">]</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">                     <span class="nf">sprintf</span><span class="p">(</span><span class="s">&#34;%4d&#34;</span><span class="p">,</span> <span class="n">cutyears[2</span><span class="o">:</span><span class="nf">length</span><span class="p">(</span><span class="n">cutyears</span><span class="p">)</span><span class="n">]</span><span class="p">),</span> <span class="n">sep</span><span class="o">=</span><span class="s">&#34;-&#34;</span><span class="p">))</span>
</span></span><span class="line"><span class="cl">    <span class="n">dataset</span> <span class="o">&lt;-</span> <span class="n">dataset</span><span class="nf">[order</span><span class="p">(</span><span class="n">dataset</span><span class="o">$</span><span class="n">period</span><span class="p">),</span><span class="n">]</span>
</span></span><span class="line"><span class="cl">    <span class="n">g</span> <span class="o">&lt;-</span> <span class="nf">leaflet</span><span class="p">()</span> <span class="o">%&gt;%</span> <span class="nf">addProviderTiles</span><span class="p">(</span><span class="n">providers</span><span class="o">$</span><span class="n">OpenStreetMap</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="nf">for </span><span class="p">(</span><span class="n">i</span> <span class="n">in</span> <span class="nf">unique</span><span class="p">(</span><span class="n">refMap</span><span class="o">$</span><span class="n">id</span><span class="p">)){</span>
</span></span><span class="line"><span class="cl">        <span class="n">g</span> <span class="o">&lt;-</span> <span class="n">g</span> <span class="o">%&gt;%</span>
</span></span><span class="line"><span class="cl">            <span class="nf">addPolygons</span><span class="p">(</span><span class="o">~</span><span class="n">long</span><span class="p">,</span> <span class="o">~</span><span class="n">lat</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">refMap[refMap</span><span class="o">$</span><span class="n">id</span><span class="o">==</span><span class="n">i</span><span class="p">,</span><span class="n">]</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">                <span class="n">label</span><span class="o">=</span><span class="n">dyn</span><span class="p">,</span> <span class="n">labelOptions</span><span class="o">=</span><span class="nf">labelOptions</span><span class="p">(</span><span class="n">textsize</span><span class="o">=</span><span class="s">&#34;20px&#34;</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">                <span class="n">weight</span><span class="o">=</span><span class="m">1</span><span class="p">,</span> <span class="n">fillColor</span><span class="o">=</span><span class="n">bgColor</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">bgColor</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="n">pal</span> <span class="o">&lt;-</span> <span class="nf">colorFactor</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">        <span class="n">viridis</span><span class="o">::</span><span class="nf">plasma</span><span class="p">(</span><span class="m">2</span><span class="o">*</span><span class="nf">nlevels</span><span class="p">(</span><span class="n">dataset</span><span class="o">$</span><span class="n">period</span><span class="p">)),</span>
</span></span><span class="line"><span class="cl">        <span class="n">levels</span><span class="o">=</span><span class="nf">levels</span><span class="p">(</span><span class="n">dataset</span><span class="o">$</span><span class="n">period</span><span class="p">),</span> <span class="n">ordered</span><span class="o">=</span><span class="kc">TRUE</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="n">g</span> <span class="o">&lt;-</span> <span class="n">g</span> <span class="o">%&gt;%</span> <span class="nf">addCircleMarkers</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">        <span class="o">~</span><span class="n">long</span><span class="p">,</span> <span class="o">~</span><span class="n">lat</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">dataset</span><span class="p">,</span> <span class="n">radius</span><span class="o">=</span><span class="m">2</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">        <span class="n">label</span><span class="o">=</span><span class="nf">paste</span><span class="p">(</span><span class="n">dataset</span><span class="o">$</span><span class="n">NameChn</span><span class="p">,</span> <span class="n">dataset</span><span class="o">$</span><span class="n">Name</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">        <span class="n">popup</span><span class="o">=</span><span class="nf">paste</span><span class="p">(</span><span class="n">dataset</span><span class="o">$</span><span class="n">EntryYear</span><span class="p">,</span> <span class="s">&#34;&lt;br&gt;&#34;</span><span class="p">,</span> <span class="n">dataset</span><span class="o">$</span><span class="n">Prov</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">                    <span class="n">dataset</span><span class="o">$</span><span class="n">AddrChn</span><span class="p">,</span> <span class="n">dataset</span><span class="o">$</span><span class="n">AddrName</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s">&#34; &#34;</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">        <span class="n">color</span><span class="o">=~</span><span class="nf">pal</span><span class="p">(</span><span class="n">period</span><span class="p">),</span> <span class="n">group</span><span class="o">=~</span><span class="n">period</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="n">g</span> <span class="o">%&gt;%</span> <span class="nf">addLayersControl</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">        <span class="n">overlayGroups</span><span class="o">=</span><span class="nf">rev</span><span class="p">(</span><span class="nf">levels</span><span class="p">(</span><span class="n">dataset</span><span class="o">$</span><span class="n">period</span><span class="p">)),</span>
</span></span><span class="line"><span class="cl">        <span class="n">options</span><span class="o">=</span><span class="nf">layersControlOptions</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">            <span class="n">collapse</span><span class="o">=</span><span class="kc">FALSE</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">        <span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="nf">make_leaflet</span><span class="p">(</span><span class="n">nsong.bou</span><span class="p">,</span> <span class="s">&#34;北宋&#34;</span><span class="p">,</span> <span class="s">&#34;red&#34;</span><span class="p">,</span> <span class="n">nsong.js</span><span class="p">,</span> <span class="nf">c</span><span class="p">(</span><span class="m">960</span><span class="p">,</span> <span class="m">1021</span><span class="p">,</span> <span class="m">1085</span><span class="p">,</span> <span class="m">1127</span><span class="p">))</span>
</span></span></code></pre></td></tr></table>
</div>
</div><iframe src="https://gh-1251443721.cos.ap-chengdu.myqcloud.com/2017/0430/song.html" width="100%" height="500"></iframe>
<p><a href="https://gh-1251443721.cos.ap-chengdu.myqcloud.com/2017/0430/song.html">点开查看源文件</a></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="nf">make_leaflet</span><span class="p">(</span><span class="n">ming.bou</span><span class="p">,</span> <span class="s">&#34;明朝&#34;</span><span class="p">,</span> <span class="s">&#34;red&#34;</span><span class="p">,</span> <span class="n">ming.js</span><span class="p">,</span> <span class="nf">c</span><span class="p">(</span><span class="m">1368</span><span class="p">,</span> <span class="m">1434</span><span class="p">,</span> <span class="m">1572</span><span class="p">,</span> <span class="m">1644</span><span class="p">))</span>
</span></span></code></pre></td></tr></table>
</div>
</div><iframe src="https://gh-1251443721.cos.ap-chengdu.myqcloud.com/2017/0430/ming.html" width="100%" height="500"></iframe>
<p><a href="https://gh-1251443721.cos.ap-chengdu.myqcloud.com/2017/0430/ming.html">点开查看源文件</a></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="nf">make_leaflet</span><span class="p">(</span><span class="n">qing.bou</span><span class="p">,</span> <span class="s">&#34;清朝&#34;</span><span class="p">,</span> <span class="s">&#34;black&#34;</span><span class="p">,</span> <span class="n">qing.js</span><span class="p">,</span> <span class="nf">c</span><span class="p">(</span><span class="m">1644</span><span class="p">,</span> <span class="m">1735</span><span class="p">,</span> <span class="m">1850</span><span class="p">,</span> <span class="m">1911</span><span class="p">))</span>
</span></span></code></pre></td></tr></table>
</div>
</div><iframe src="https://gh-1251443721.cos.ap-chengdu.myqcloud.com/2017/0430/qing.html" width="100%" height="500"></iframe>
<p><a href="https://gh-1251443721.cos.ap-chengdu.myqcloud.com/2017/0430/qing.html">点开查看源文件</a></p>
<h4 id="古今地名一致性">古今地名一致性</h4>
<p>前次分析，直接把所有县级地名拿出来跑频数，第一名是莆田。这当然很不精准——地名一直会变嘛。像南京这种历史上频繁改名的地方，就会吃很大亏。最好统一起来。找地名的历史沿革数据库不是很容易，干脆统一到今名如何？（请都说“好”）</p>
<p>统一到今名有三个好处：</p>
<ol>
<li>能按照今天的区划统计哪个省市出了更多进士（其实谁稀罕这个）；</li>
<li>便于我操作（-口_口-：仄寺坠重要的）；</li>
<li>能引出一个知识点：sp::over()。</li>
</ol>
<p>既然我们有每个省/市级空间多边形数据，又有每个进士的位置坐标，恰好又都是同一套大地坐标系测量值，历史沿革问题不就转化成了一个纯粹的GIS转换问题吗？只要用算法评价每个点落在哪个多边形内，它自然就属于这个行政区划。</p>
<p>这个算法有现成的，就是sp包的over函数。</p>
<p>构造一个<code>which_polygon</code>函数，把一个点轮流放进大陆和台湾的多边形数据集里，看它到底落在哪个多边形，输出这个多边形的元数据。注意：数据都得是sp包的工作类型，比如SpatialPoints、SpatialPolygons之类。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="n">which_polygon</span> <span class="o">&lt;-</span> <span class="nf">function</span><span class="p">(</span><span class="n">point</span><span class="p">,</span> <span class="n">cn.cities</span><span class="p">,</span> <span class="n">tw.cities</span><span class="p">){</span>
</span></span><span class="line"><span class="cl">    <span class="nf">library</span><span class="p">(</span><span class="n">sp</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="n">out</span> <span class="o">&lt;-</span> <span class="nf">over</span><span class="p">(</span><span class="nf">SpatialPoints</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">        <span class="nf">matrix</span><span class="p">(</span><span class="n">point</span><span class="p">,</span> <span class="n">nrow</span><span class="o">=</span><span class="m">1</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">        <span class="n">proj4string</span> <span class="o">=</span> <span class="n">cn.cities</span><span class="o">@</span><span class="n">proj4string</span><span class="p">),</span> <span class="n">cn.cities</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="nf">if </span><span class="p">(</span><span class="nf">is.na</span><span class="p">(</span><span class="n">out</span><span class="o">$</span><span class="n">ID_0</span><span class="p">))</span>
</span></span><span class="line"><span class="cl">        <span class="n">out</span> <span class="o">&lt;-</span> <span class="nf">over</span><span class="p">(</span><span class="nf">SpatialPoints</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">            <span class="nf">matrix</span><span class="p">(</span><span class="n">point</span><span class="p">,</span> <span class="n">nrow</span><span class="o">=</span><span class="m">1</span><span class="p">),</span>
</span></span><span class="line"><span class="cl">            <span class="n">proj4string</span> <span class="o">=</span> <span class="n">tw.cities</span><span class="o">@</span><span class="n">proj4string</span><span class="p">),</span> <span class="n">tw.cities</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="nf">return</span><span class="p">(</span><span class="n">out</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>每个进士个体都丢进去算太耗时，我们先统计下频数。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="nf">library</span><span class="p">(</span><span class="n">data.table</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">nsong.js.stat</span> <span class="o">&lt;-</span> <span class="nf">dcast</span><span class="p">(</span><span class="n">nsong.js</span><span class="p">,</span> <span class="n">x_coord</span><span class="o">+</span><span class="n">y_coord</span><span class="o">~</span><span class="n">.,</span> <span class="n">length</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">ming.js.stat</span> <span class="o">&lt;-</span> <span class="nf">dcast</span><span class="p">(</span><span class="n">ming.js</span><span class="p">,</span> <span class="n">x_coord</span><span class="o">+</span><span class="n">y_coord</span><span class="o">~</span><span class="n">.,</span> <span class="n">length</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">qing.js.stat</span> <span class="o">&lt;-</span> <span class="nf">dcast</span><span class="p">(</span><span class="n">qing.js</span><span class="p">,</span> <span class="n">x_coord</span><span class="o">+</span><span class="n">y_coord</span><span class="o">~</span><span class="n">.,</span> <span class="n">length</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>再次动用并行计算parallel。因为要按行遍历矩阵，所以要用<code>apply</code>的并行版本<code>parApply</code>。获得结果是个list，需要再do.call调用bind_rows，合并成一个数据框。当然还有种办法是直接改写which_polygon，把进士位置整理成SpatialPointsDataFrame，一次性丢进去，效率上可能更高。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="nf">library</span><span class="p">(</span><span class="n">parallel</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">cl</span> <span class="o">&lt;-</span> <span class="nf">makeCluster</span><span class="p">(</span><span class="nf">getOption</span><span class="p">(</span><span class="s">&#34;cl.cores&#34;</span><span class="p">,</span> <span class="m">2</span><span class="p">))</span>
</span></span><span class="line"><span class="cl"><span class="n">nsong.js.belong</span> <span class="o">&lt;-</span> <span class="nf">parApply</span><span class="p">(</span><span class="n">cl</span><span class="p">,</span> <span class="n">nsong.js.stat[</span><span class="p">,</span><span class="nf">c</span><span class="p">(</span><span class="s">&#34;x_coord&#34;</span><span class="p">,</span> <span class="s">&#34;y_coord&#34;</span><span class="p">)</span><span class="n">]</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">                            <span class="m">1</span><span class="p">,</span> <span class="n">which_polygon</span><span class="p">,</span> <span class="n">cn.cities</span><span class="p">,</span> <span class="n">tw.cities</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">ming.js.belong</span> <span class="o">&lt;-</span> <span class="nf">parApply</span><span class="p">(</span><span class="n">cl</span><span class="p">,</span> <span class="n">ming.js.stat[</span><span class="p">,</span><span class="nf">c</span><span class="p">(</span><span class="s">&#34;x_coord&#34;</span><span class="p">,</span> <span class="s">&#34;y_coord&#34;</span><span class="p">)</span><span class="n">]</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">                           <span class="m">1</span><span class="p">,</span> <span class="n">which_polygon</span><span class="p">,</span> <span class="n">cn.cities</span><span class="p">,</span> <span class="n">tw.cities</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">qing.js.belong</span> <span class="o">&lt;-</span> <span class="nf">parApply</span><span class="p">(</span><span class="n">cl</span><span class="p">,</span> <span class="n">qing.js.stat[</span><span class="p">,</span><span class="nf">c</span><span class="p">(</span><span class="s">&#34;x_coord&#34;</span><span class="p">,</span> <span class="s">&#34;y_coord&#34;</span><span class="p">)</span><span class="n">]</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">                           <span class="m">1</span><span class="p">,</span> <span class="n">which_polygon</span><span class="p">,</span> <span class="n">cn.cities</span><span class="p">,</span> <span class="n">tw.cities</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nf">stopCluster</span><span class="p">(</span><span class="n">cl</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nf">library</span><span class="p">(</span><span class="n">dplyr</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">nsong.js.belong</span> <span class="o">&lt;-</span> <span class="nf">do.call</span><span class="p">(</span><span class="s">&#39;bind_rows&#39;</span><span class="p">,</span> <span class="n">nsong.js.belong</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">ming.js.belong</span> <span class="o">&lt;-</span> <span class="nf">do.call</span><span class="p">(</span><span class="s">&#39;bind_rows&#39;</span><span class="p">,</span> <span class="n">ming.js.belong</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">qing.js.belong</span> <span class="o">&lt;-</span> <span class="nf">do.call</span><span class="p">(</span><span class="s">&#39;bind_rows&#39;</span><span class="p">,</span> <span class="n">qing.js.belong</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">nsong.js.belong</span><span class="o">$</span><span class="n">num</span> <span class="o">&lt;-</span> <span class="n">nsong.js.stat</span><span class="o">$</span><span class="n">`.`</span>
</span></span><span class="line"><span class="cl"><span class="n">ming.js.belong</span><span class="o">$</span><span class="n">num</span> <span class="o">&lt;-</span> <span class="n">ming.js.stat</span><span class="o">$</span><span class="n">`.`</span>
</span></span><span class="line"><span class="cl"><span class="n">qing.js.belong</span><span class="o">$</span><span class="n">num</span> <span class="o">&lt;-</span> <span class="n">qing.js.stat</span><span class="o">$</span><span class="n">`.`</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h4 id="进士数排名">进士数排名</h4>
<p>把三个朝代的数据集做一下深加工，合并到一起（DYNASTY列标识朝代属性）。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="nf">library</span><span class="p">(</span><span class="n">data.table</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">nsong.js.belong</span> <span class="o">&lt;-</span> <span class="nf">dcast</span><span class="p">(</span><span class="n">nsong.js.belong</span><span class="p">,</span> <span class="n">NAME_1</span><span class="o">+</span><span class="n">NAME_2</span><span class="o">+</span><span class="n">NL_NAME_2</span><span class="o">~</span><span class="n">.,</span> <span class="n">sum</span><span class="p">,</span> <span class="n">value.var</span><span class="o">=</span><span class="s">&#34;num&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">nsong.js.belong</span><span class="o">$</span><span class="n">DYNASTY</span> <span class="o">&lt;-</span> <span class="s">&#34;北宋&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">ming.js.belong</span> <span class="o">&lt;-</span> <span class="nf">dcast</span><span class="p">(</span><span class="n">ming.js.belong</span><span class="p">,</span> <span class="n">NAME_1</span><span class="o">+</span><span class="n">NAME_2</span><span class="o">+</span><span class="n">NL_NAME_2</span><span class="o">~</span><span class="n">.,</span> <span class="n">sum</span><span class="p">,</span> <span class="n">value.var</span><span class="o">=</span><span class="s">&#34;num&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">ming.js.belong</span><span class="o">$</span><span class="n">DYNASTY</span> <span class="o">&lt;-</span> <span class="s">&#34;明朝&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">qing.js.belong</span> <span class="o">&lt;-</span> <span class="nf">dcast</span><span class="p">(</span><span class="n">qing.js.belong</span><span class="p">,</span> <span class="n">NAME_1</span><span class="o">+</span><span class="n">NAME_2</span><span class="o">+</span><span class="n">NL_NAME_2</span><span class="o">~</span><span class="n">.,</span> <span class="n">sum</span><span class="p">,</span> <span class="n">value.var</span><span class="o">=</span><span class="s">&#34;num&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">qing.js.belong</span><span class="o">$</span><span class="n">DYNASTY</span> <span class="o">&lt;-</span> <span class="s">&#34;清朝&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nf">library</span><span class="p">(</span><span class="n">dplyr</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">js.belong</span> <span class="o">&lt;-</span> <span class="nf">do.call</span><span class="p">(</span><span class="s">&#34;bind_rows&#34;</span><span class="p">,</span> <span class="nf">list</span><span class="p">(</span><span class="n">nsong.js.belong</span><span class="p">,</span> <span class="n">ming.js.belong</span><span class="p">,</span> <span class="n">qing.js.belong</span><span class="p">))</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>再清洗一下数据，把英文地名里的无关符号删掉（“内江”出现了两种写法），再把0都转为NA（可视化时不要涂色）。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="n">js.belong</span><span class="o">$</span><span class="n">NAME_2</span> <span class="o">&lt;-</span> <span class="n">stringr</span><span class="o">::</span><span class="nf">str_replace</span><span class="p">(</span><span class="n">js.belong</span><span class="o">$</span><span class="n">NAME_2</span><span class="p">,</span> <span class="s">&#34;^([^]]+)\\]+$&#34;</span><span class="p">,</span> <span class="s">&#34;\\1&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">js.belong</span><span class="o">$</span><span class="n">.[js.belong</span><span class="o">$</span><span class="n">.=</span><span class="o">=</span><span class="m">0</span><span class="n">]</span> <span class="o">&lt;-</span> <span class="kc">NA</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>排名来了。</p>
<h5 id="先是市级排名">先是市级排名</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="n">js.order</span> <span class="o">&lt;-</span> <span class="nf">dcast</span><span class="p">(</span><span class="n">js.belong</span><span class="p">,</span> <span class="n">NAME_1</span><span class="o">+</span><span class="n">NAME_2</span><span class="o">+</span><span class="n">NL_NAME_2</span><span class="o">~</span><span class="n">DYNASTY</span><span class="p">,</span> <span class="n">sum</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">                  <span class="n">value.var</span><span class="o">=</span><span class="s">&#34;.&#34;</span><span class="p">,</span> <span class="n">margins</span><span class="o">=</span><span class="s">&#34;DYNASTY&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">knitr</span><span class="o">::</span><span class="nf">kable</span><span class="p">(</span><span class="n">js.order</span><span class="nf">[order</span><span class="p">(</span><span class="n">js.order</span><span class="o">$</span><span class="nf">`</span><span class="p">(</span><span class="n">all</span><span class="p">)</span><span class="n">`</span><span class="p">,</span> <span class="n">decreasing</span><span class="o">=</span><span class="kc">TRUE</span><span class="p">),</span><span class="n">]</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>地名归集后，第一名是苏州，然后是福州，第三才是莆田，第四是杭州。前四名实际难分伯仲。</p>
<table>
<thead>
<tr>
<th style="text-align:right">序号</th>
<th>NAME_1</th>
<th>NAME_2</th>
<th>NL_NAME_2</th>
<th style="text-align:right">北宋</th>
<th style="text-align:right">明朝</th>
<th style="text-align:right">清朝</th>
<th style="text-align:right">(all)</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:right">1</td>
<td>Jiangsu</td>
<td>Suzhou</td>
<td>苏州市</td>
<td style="text-align:right">157</td>
<td style="text-align:right">60</td>
<td style="text-align:right">179</td>
<td style="text-align:right">396</td>
</tr>
<tr>
<td style="text-align:right">2</td>
<td>Fujian</td>
<td>Fuzhou</td>
<td>福州市</td>
<td style="text-align:right">273</td>
<td style="text-align:right">36</td>
<td style="text-align:right">83</td>
<td style="text-align:right">392</td>
</tr>
<tr>
<td style="text-align:right">3</td>
<td>Fujian</td>
<td>Putian</td>
<td>莆田市</td>
<td style="text-align:right">264</td>
<td style="text-align:right">109</td>
<td style="text-align:right">8</td>
<td style="text-align:right">381</td>
</tr>
<tr>
<td style="text-align:right">4</td>
<td>Zhejiang</td>
<td>Hangzhou</td>
<td>杭州市</td>
<td style="text-align:right">115</td>
<td style="text-align:right">38</td>
<td style="text-align:right">221</td>
<td style="text-align:right">374</td>
</tr>
<tr>
<td style="text-align:right">5</td>
<td>Fujian</td>
<td>Nanping</td>
<td>南平市</td>
<td style="text-align:right">224</td>
<td style="text-align:right">15</td>
<td style="text-align:right">7</td>
<td style="text-align:right">246</td>
</tr>
<tr>
<td style="text-align:right">6</td>
<td>Jiangxi</td>
<td>Ji’an</td>
<td>吉安市</td>
<td style="text-align:right">128</td>
<td style="text-align:right">97</td>
<td style="text-align:right">11</td>
<td style="text-align:right">236</td>
</tr>
<tr>
<td style="text-align:right">7</td>
<td>Zhejiang</td>
<td>Ningbo</td>
<td>宁波市</td>
<td style="text-align:right">94</td>
<td style="text-align:right">97</td>
<td style="text-align:right">39</td>
<td style="text-align:right">230</td>
</tr>
<tr>
<td style="text-align:right">8</td>
<td>Jiangsu</td>
<td>Changzhou</td>
<td>常州市</td>
<td style="text-align:right">97</td>
<td style="text-align:right">20</td>
<td style="text-align:right">97</td>
<td style="text-align:right">214</td>
</tr>
<tr>
<td style="text-align:right">9</td>
<td>Zhejiang</td>
<td>Jiaxing</td>
<td>嘉兴市</td>
<td style="text-align:right">19</td>
<td style="text-align:right">68</td>
<td style="text-align:right">117</td>
<td style="text-align:right">204</td>
</tr>
<tr>
<td style="text-align:right">10</td>
<td>Jiangxi</td>
<td>Fuzhou</td>
<td>抚州市</td>
<td style="text-align:right">120</td>
<td style="text-align:right">27</td>
<td style="text-align:right">52</td>
<td style="text-align:right">199</td>
</tr>
<tr>
<td style="text-align:right">11</td>
<td>Zhejiang</td>
<td>Huzhou</td>
<td>湖州市</td>
<td style="text-align:right">86</td>
<td style="text-align:right">17</td>
<td style="text-align:right">78</td>
<td style="text-align:right">181</td>
</tr>
<tr>
<td style="text-align:right">12</td>
<td>Zhejiang</td>
<td>Shaoxing</td>
<td>绍兴市</td>
<td style="text-align:right">75</td>
<td style="text-align:right">48</td>
<td style="text-align:right">51</td>
<td style="text-align:right">174</td>
</tr>
<tr>
<td style="text-align:right">13</td>
<td>Fujian</td>
<td>Quanzhou</td>
<td>泉州市</td>
<td style="text-align:right">110</td>
<td style="text-align:right">28</td>
<td style="text-align:right">31</td>
<td style="text-align:right">169</td>
</tr>
<tr>
<td style="text-align:right">14</td>
<td>Jiangsu</td>
<td>Wuxi</td>
<td>无锡市</td>
<td style="text-align:right">73</td>
<td style="text-align:right">34</td>
<td style="text-align:right">55</td>
<td style="text-align:right">162</td>
</tr>
<tr>
<td style="text-align:right">15</td>
<td>Jiangxi</td>
<td>Shangrao</td>
<td>上饶市</td>
<td style="text-align:right">93</td>
<td style="text-align:right">26</td>
<td style="text-align:right">28</td>
<td style="text-align:right">147</td>
</tr>
</tbody>
</table>
<p>给张图，同样是基于leaflet的。注意：<strong>文件非常很大，极费流量</strong>。</p>
<iframe src="https://gh-1251443721.cos.ap-chengdu.myqcloud.com/2017/0430/city.html" width="100%" height="500"></iframe>
<p><a href="https://gh-1251443721.cos.ap-chengdu.myqcloud.com/2017/0430/city.html">点开查看源文件</a></p>
<h5 id="然后是省级排名">然后是省级排名</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="n">js.order.prov</span> <span class="o">&lt;-</span> <span class="nf">dcast</span><span class="p">(</span><span class="n">js.belong</span><span class="p">,</span> <span class="n">NAME_1</span><span class="o">~</span><span class="n">DYNASTY</span><span class="p">,</span> <span class="n">sum</span><span class="p">,</span> <span class="n">value.var</span><span class="o">=</span><span class="s">&#34;.&#34;</span><span class="p">,</span> <span class="n">margins</span><span class="o">=</span><span class="s">&#34;DYNASTY&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">knitr</span><span class="o">::</span><span class="nf">kable</span><span class="p">(</span><span class="n">js.order.prov</span><span class="nf">[order</span><span class="p">(</span><span class="n">js.order.prov</span><span class="o">$</span><span class="nf">`</span><span class="p">(</span><span class="n">all</span><span class="p">)</span><span class="n">`</span><span class="p">,</span> <span class="n">decreasing</span><span class="o">=</span><span class="kc">TRUE</span><span class="p">),</span><span class="n">]</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><table>
<thead>
<tr>
<th style="text-align:right">序号</th>
<th>NAME_1</th>
<th style="text-align:right">北宋</th>
<th style="text-align:right">明朝</th>
<th style="text-align:right">清朝</th>
<th style="text-align:right">(all)</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:right">1</td>
<td>Zhejiang</td>
<td style="text-align:right">636</td>
<td style="text-align:right">337</td>
<td style="text-align:right">530</td>
<td style="text-align:right">1503</td>
</tr>
<tr>
<td style="text-align:right">2</td>
<td>Fujian</td>
<td style="text-align:right">950</td>
<td style="text-align:right">202</td>
<td style="text-align:right">147</td>
<td style="text-align:right">1299</td>
</tr>
<tr>
<td style="text-align:right">3</td>
<td>Jiangsu</td>
<td style="text-align:right">476</td>
<td style="text-align:right">179</td>
<td style="text-align:right">536</td>
<td style="text-align:right">1191</td>
</tr>
<tr>
<td style="text-align:right">4</td>
<td>Jiangxi</td>
<td style="text-align:right">559</td>
<td style="text-align:right">233</td>
<td style="text-align:right">232</td>
<td style="text-align:right">1024</td>
</tr>
<tr>
<td style="text-align:right">5</td>
<td>Henan</td>
<td style="text-align:right">276</td>
<td style="text-align:right">97</td>
<td style="text-align:right">185</td>
<td style="text-align:right">558</td>
</tr>
<tr>
<td style="text-align:right">6</td>
<td>Shandong</td>
<td style="text-align:right">99</td>
<td style="text-align:right">88</td>
<td style="text-align:right">311</td>
<td style="text-align:right">498</td>
</tr>
<tr>
<td style="text-align:right">7</td>
<td>Anhui</td>
<td style="text-align:right">142</td>
<td style="text-align:right">68</td>
<td style="text-align:right">197</td>
<td style="text-align:right">407</td>
</tr>
<tr>
<td style="text-align:right">8</td>
<td>Sichuan</td>
<td style="text-align:right">269</td>
<td style="text-align:right">30</td>
<td style="text-align:right">65</td>
<td style="text-align:right">364</td>
</tr>
<tr>
<td style="text-align:right">9</td>
<td>Hebei</td>
<td style="text-align:right">27</td>
<td style="text-align:right">86</td>
<td style="text-align:right">200</td>
<td style="text-align:right">313</td>
</tr>
<tr>
<td style="text-align:right">10</td>
<td>Hunan</td>
<td style="text-align:right">79</td>
<td style="text-align:right">27</td>
<td style="text-align:right">152</td>
<td style="text-align:right">258</td>
</tr>
<tr>
<td style="text-align:right">11</td>
<td>Hubei</td>
<td style="text-align:right">31</td>
<td style="text-align:right">52</td>
<td style="text-align:right">133</td>
<td style="text-align:right">216</td>
</tr>
<tr>
<td style="text-align:right">12</td>
<td>Shanxi</td>
<td style="text-align:right">24</td>
<td style="text-align:right">63</td>
<td style="text-align:right">125</td>
<td style="text-align:right">212</td>
</tr>
<tr>
<td style="text-align:right">13</td>
<td>Guangdong</td>
<td style="text-align:right">40</td>
<td style="text-align:right">54</td>
<td style="text-align:right">105</td>
<td style="text-align:right">199</td>
</tr>
<tr>
<td style="text-align:right">14</td>
<td>Shaanxi</td>
<td style="text-align:right">18</td>
<td style="text-align:right">49</td>
<td style="text-align:right">89</td>
<td style="text-align:right">156</td>
</tr>
<tr>
<td style="text-align:right">15</td>
<td>Shanghai</td>
<td style="text-align:right">26</td>
<td style="text-align:right">47</td>
<td style="text-align:right">68</td>
<td style="text-align:right">141</td>
</tr>
<tr>
<td style="text-align:right">16</td>
<td>Beijing</td>
<td style="text-align:right">2</td>
<td style="text-align:right">12</td>
<td style="text-align:right">115</td>
<td style="text-align:right">129</td>
</tr>
<tr>
<td style="text-align:right">17</td>
<td>Yunnan</td>
<td style="text-align:right">0</td>
<td style="text-align:right">1</td>
<td style="text-align:right">69</td>
<td style="text-align:right">70</td>
</tr>
<tr>
<td style="text-align:right">18</td>
<td>Guangxi</td>
<td style="text-align:right">2</td>
<td style="text-align:right">10</td>
<td style="text-align:right">56</td>
<td style="text-align:right">68</td>
</tr>
<tr>
<td style="text-align:right">19</td>
<td>Tianjin</td>
<td style="text-align:right">0</td>
<td style="text-align:right">2</td>
<td style="text-align:right">61</td>
<td style="text-align:right">63</td>
</tr>
<tr>
<td style="text-align:right">20</td>
<td>Guizhou</td>
<td style="text-align:right">0</td>
<td style="text-align:right">4</td>
<td style="text-align:right">51</td>
<td style="text-align:right">55</td>
</tr>
<tr>
<td style="text-align:right">21</td>
<td>Chongqing</td>
<td style="text-align:right">8</td>
<td style="text-align:right">7</td>
<td style="text-align:right">24</td>
<td style="text-align:right">39</td>
</tr>
<tr>
<td style="text-align:right">22</td>
<td>Gansu</td>
<td style="text-align:right">6</td>
<td style="text-align:right">6</td>
<td style="text-align:right">19</td>
<td style="text-align:right">31</td>
</tr>
<tr>
<td style="text-align:right">23</td>
<td>Hainan</td>
<td style="text-align:right">0</td>
<td style="text-align:right">9</td>
<td style="text-align:right">3</td>
<td style="text-align:right">12</td>
</tr>
<tr>
<td style="text-align:right">24</td>
<td>Liaoning</td>
<td style="text-align:right">0</td>
<td style="text-align:right">0</td>
<td style="text-align:right">12</td>
<td style="text-align:right">12</td>
</tr>
<tr>
<td style="text-align:right">25</td>
<td>Jilin</td>
<td style="text-align:right">0</td>
<td style="text-align:right">0</td>
<td style="text-align:right">8</td>
<td style="text-align:right">8</td>
</tr>
<tr>
<td style="text-align:right">26</td>
<td>Ningxia Hui</td>
<td style="text-align:right">0</td>
<td style="text-align:right">0</td>
<td style="text-align:right">7</td>
<td style="text-align:right">7</td>
</tr>
<tr>
<td style="text-align:right">27</td>
<td>Taiwan</td>
<td style="text-align:right">0</td>
<td style="text-align:right">0</td>
<td style="text-align:right">2</td>
<td style="text-align:right">2</td>
</tr>
</tbody>
</table>
<p>同样给张图。这次是基于ECharts2的。由于这个包是我写的，所以我要额外安利一下<a href="http://madlogos.github.io/recharts">http://madlogos.github.io/recharts</a>。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-r" data-lang="r"><span class="line"><span class="cl"><span class="nf">library</span><span class="p">(</span><span class="n">recharts</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">dict</span> <span class="o">&lt;-</span> <span class="n">geoNameMap[geoNameMap</span><span class="o">$</span><span class="n">FKEY</span><span class="o">==</span><span class="m">31</span><span class="p">,</span> <span class="nf">c</span><span class="p">(</span><span class="s">&#34;EN&#34;</span><span class="p">,</span> <span class="s">&#34;CN&#34;</span><span class="p">)</span><span class="n">]</span>
</span></span><span class="line"><span class="cl"><span class="n">js.order.prov</span> <span class="o">&lt;-</span> <span class="nf">merge</span><span class="p">(</span><span class="n">js.order.prov</span><span class="p">,</span> <span class="n">dict</span><span class="p">,</span> <span class="n">by.x</span><span class="o">=</span><span class="s">&#34;NAME_1&#34;</span><span class="p">,</span> <span class="n">by.y</span><span class="o">=</span><span class="s">&#34;EN&#34;</span><span class="p">,</span> <span class="n">all.x</span><span class="o">=</span><span class="kc">TRUE</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">js.order.ec</span> <span class="o">&lt;-</span> <span class="nf">melt</span><span class="p">(</span><span class="n">js.order.prov[</span><span class="p">,</span><span class="nf">c</span><span class="p">(</span><span class="m">2</span><span class="o">:</span><span class="m">4</span><span class="p">,</span><span class="m">6</span><span class="p">)</span><span class="n">]</span><span class="p">,</span> <span class="n">id</span><span class="o">=</span><span class="s">&#34;CN&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">js.order.ec</span><span class="o">$</span><span class="n">value[js.order.ec</span><span class="o">$</span><span class="n">value</span><span class="o">==</span><span class="m">0</span><span class="n">]</span> <span class="o">&lt;-</span> <span class="kc">NA</span>
</span></span><span class="line"><span class="cl"><span class="nf">echartR</span><span class="p">(</span><span class="n">js.order.ec</span><span class="p">,</span> <span class="n">CN</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">t</span><span class="o">=</span><span class="n">variable</span><span class="p">,</span> <span class="n">type</span><span class="o">=</span><span class="s">&#34;map_china&#34;</span><span class="p">,</span> <span class="n">subtype</span><span class="o">=</span><span class="s">&#34;average&#34;</span><span class="p">)</span> <span class="o">%&gt;%</span>
</span></span><span class="line"><span class="cl">    <span class="nf">setDataRange</span><span class="p">(</span><span class="n">splitNumber</span><span class="o">=</span><span class="m">0</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="nf">c</span><span class="p">(</span><span class="s">&#39;darkblue&#39;</span><span class="p">,</span><span class="s">&#39;cyan&#39;</span><span class="p">))</span> <span class="o">%&gt;%</span>
</span></span><span class="line"><span class="cl">    <span class="nf">setTitle</span><span class="p">(</span><span class="s">&#34;各省进士数&#34;</span><span class="p">,</span> <span class="n">pos</span><span class="o">=</span><span class="m">11</span><span class="p">)</span> <span class="o">%&gt;%</span>
</span></span><span class="line"><span class="cl">    <span class="nf">setTimeline</span><span class="p">(</span><span class="n">autoPlay</span><span class="o">=</span><span class="kc">TRUE</span><span class="p">)</span> <span class="o">%&gt;%</span> <span class="nf">setLegend</span><span class="p">(</span><span class="kc">FALSE</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><iframe src="https://gh-1251443721.cos.ap-chengdu.myqcloud.com/2017/0430/prov.html" width="640" height="500"></iframe>
<p><a href="https://gh-1251443721.cos.ap-chengdu.myqcloud.com/2017/0430/prov.html">点开查看源文件</a></p>
<p>图动起来了。感觉棒呆。</p>
<p>自己试着去和它们交互吧。</p>
<p>[完]</p>
<hr>
<!-- {% raw %} -->
<figure class="center"><img src="https://gh-1251443721.cos.ap-chengdu.myqcloud.com/QRcode.jpg"
         alt="扫码关注" width="30%"/><figcaption>
            <h4>扫码关注我的公众号</h4>
        </figcaption>
</figure>
<!-- {% endraw %} -->
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">迷幻主义搬砖号子</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2021-05-09
        <a href="https://github.com/madlogos/hugo-blog.git/commit/f3e8c5ae54f54c3eea5e759b3bce5a3facc97c88" title="changes on 2021年 5月 9日 星期日 21时28分26秒 CST">(f3e8c5a)</a>
    </span>
  </p>
  <p class="copyright-item">
      <span class="item-title">原始文档</span>
      <span class="item-content"><a class="link-to-markdown" href="https://madlogos.github.io/post/song_ming_qing_jinshi_interactive/index.md" target="_blank">查看本文 Markdown 版本 »</a></span>
    </p>
  <p class="copyright-item">
    <span class="item-title">许可协议</span>
    <span class="item-content"><a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank">CC BY-NC-ND 4.0</a></span>
  </p>
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/reward/wechat.png">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/reward/alipay.png">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/%E8%BF%9B%E5%A3%AB/">进士</a>
          <a href="/tags/%E5%9C%B0%E7%90%86%E5%88%86%E6%9E%90/">地理分析</a>
          <a href="/tags/%E4%BA%A4%E4%BA%92%E5%9B%BE/">交互图</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/imacros_zyb_cnaps_scrapy/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">iMacros脚本运行三天，爬取了40万行现代化支付行号</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/song_ming_qing_jinshi/">
            <span class="next-text nav-default">北宋、明清进士空间分析</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        <div id="disqus_thread"></div>
    <script type="text/javascript">
    (function() {
      
      
      if (window.location.hostname === 'localhost') return;

      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      var disqus_shortname = 'madlogos-gh';
      dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
    </script>
    <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:madlogos@gmail.com" class="iconfont icon-email" title="email"></a>
      <a href="https://stackoverflow.com/users/5455754/madlogos?tab=profile" class="iconfont icon-stack-overflow" title="stack-overflow"></a>
      <a href="https://twitter.com/madlogos" class="iconfont icon-twitter" title="twitter"></a>
      <a href="https://www.facebook.com/madlogos" class="iconfont icon-facebook" title="facebook"></a>
      <a href="http://www.linkedin.com/in/yiying-wang/" class="iconfont icon-linkedin" title="linkedin"></a>
      <a href="https://plus.google.com/&#43;%E6%B1%AA%E8%BD%B6%E9%A2%96madlogos" class="iconfont icon-google" title="google"></a>
      <a href="http://github.com/madlogos" class="iconfont icon-github" title="github"></a>
      <a href="https://weibo.com/madlogos/" class="iconfont icon-weibo" title="weibo"></a>
      <a href="https://www.zhihu.com/people/madlogos/" class="iconfont icon-zhihu" title="zhihu"></a>
      <a href="https://www.douban.com/people/Jandeaux/" class="iconfont icon-douban" title="douban"></a>
      <a href="https://jandeaux.tumblr.com" class="iconfont icon-tumblr" title="tumblr"></a>
      <a href="https://www.instagram.com/jandeaux/" class="iconfont icon-instagram" title="instagram"></a>
      <a href="https://gitlab.com/madlogos" class="iconfont icon-gitlab" title="gitlab"></a>
      <a href="https://space.bilibili.com/384080442" class="iconfont icon-bilibili" title="bilibili"></a>
  <a href="https://madlogos.github.io/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
      <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    2017 - 
    2022
    
    <span class="author">madlogos</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script><script></script><script src="https://cdn.jsdelivr.net/npm/raphael@2.3.0/raphael.min.js" integrity="sha256-TabprKdeNXbSesCWLMrcbWSDzUhpAdcNPe5Q53rn9Yg=" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/flowchart.js@1.12.1/release/flowchart.min.js" integrity="sha256-ANSuVJkHZftRURALG24omixaZG+Sb51/+JY6EDa7MdE=" crossorigin="anonymous"></script><script></script><script src="https://cdn.jsdelivr.net/npm/webfontloader@1.6.28/webfontloader.js" integrity="sha256-4O4pS1SH31ZqrSO2A/2QJTVjTPqVe+jnYgOWUVr7EEc=" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/snapsvg@0.5.1/dist/snap.svg-min.js" integrity="sha256-oI+elz+sIm+jpn8F/qEspKoKveTc5uKeFHNNVexe6d8=" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/underscore@1.10.2/underscore-min.js" integrity="sha256-av1TvywtZ4ZqyCj/6HdtCHSJdn80HAzTgEBTJt/O8uc=" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/@rokt33r/js-sequence-diagrams@2.0.6-2/dist/sequence-diagram-min.js" integrity="sha256-eadHf9g1REH9Wvp2FLV/D9vKNvQUFKuVPgWFvmMQxBE=" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@rokt33r/js-sequence-diagrams@2.0.6-2/dist/sequence-diagram-min.css" integrity="sha256-On01v36B8LRDuL2tqhqs7Gb3Cm/NIpsLFy4OarOodUA=" crossorigin="anonymous">



<script type="text/javascript" src="/js/main.min.4ae89da218555efa0e7093a20b92017d2e1202b66fff9fc2edf4cb8d44b44c6e.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        tags: 'ams',
        }
    };
  </script>
  <script async src="https://cdn.jsdelivr.net/npm/mathjax@3.0.5/es5/tex-mml-chtml.js" integrity="sha256-HGLuEfFcsUJGhvB8cQ8nr0gai9EucOOaIxFw7qxmd+w=" crossorigin="anonymous"></script>


<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
	window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
	ga('create', 'UA-68964085-2', 'auto');
	ga('set', 'anonymizeIp', true);
	ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<script id="baidu_analytics">
  var _hmt = _hmt || [];
  (function() {
    if (window.location.hostname === 'localhost') return;
    var hm = document.createElement("script"); hm.async = true;
    hm.src = "https://hm.baidu.com/hm.js?f221e23428218df65adeefe00f85e7ed";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
</script>

<script id="baidu_push">
  (function(){
    if (window.location.hostname === 'localhost') return;
    var bp = document.createElement('script'); bp.async = true;
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
      bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
      bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
  })();
</script>


<script src="/js/custom.js"></script>


</body>
</html>
